package week2;

import org.junit.Test;

import java.util.*;

public class StringDemo {
    @Test
    public void t1(){
        String str1 = "abc";  //常量字符串,也是一个对象，对象赋给变量
        String str2 = new String("abc");
        String str3 = "abc";
        System.out.println(str1 == str2);  //false  ==比较地址
        System.out.println(str1 == str3);   //true

        System.out.println(str1.equals(str2));  //true  比较对象中的内容

        String str = "akfjgoiwgrewoi";
        System.out.println(str.length());
        char[] array = str.toCharArray();
        System.out.println(str.charAt(5));//求得是字符，位置是5
        System.out.println(str.indexOf('j'));//求得是下标位置，字符是'j'

        System.out.println(str.startsWith("jgo",3));//从数组位置3开始的字符串是否以前面字符串开头

        char[] array1 = new char[]{'a','b','d'};
        String str31 = new String(array1);
        System.out.println(str31);

        String st1 = str.replace('j','h');
        System.out.println(st1);
        String[] gos = str.split("go");
        for (String a : gos){
            System.out.println(a);
        }



    }

    @Test
    public void t3(){
        String str1 = "999";
        String str2 = "223";
        String str3 = "956";
        str1 = str2 + str3;
        System.out.println(str1);

        StringBuffer stringBuffer = new StringBuffer("abc");
        stringBuffer.append(str1).append(str2);
        System.out.println(stringBuffer);

        StringBuilder stringBuilder = new StringBuilder("asd");
        stringBuilder.append(str1).append(str2);
        System.out.println(stringBuilder);

    }

    @Test
    public void t4() {
        Student student1 = new Student(1,"sdfg",230);
        Student student2 = new Student(2,"sdfg",230);
        Student student3 = new Student(3,"sdfg",230);
        ArrayList<Student> list = new ArrayList<Student>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        for (int i = 0; i < list.size(); i++){
            Student student = list.get(i);
            System.out.println(student);
        }
        for (Student student : list){
            System.out.println(student);
        }

        HashSet<Student> set = new HashSet<Student>();
        set.add(student1);
        set.add(student1);
        set.add(student2);
        set.add(student3);
        for (Student student : set){
            System.out.println(student);
        }

    }

    @Test
    public void t66() {
        //map是键值对<key,value>将put的内容封装成Entry类
        HashMap<String, String> map = new HashMap<>();
        map.put("cn", "中国");
        map.put("us", "美国");
        //Map.Entry<String,String> entry;
        //遍历map集合
        Set<Map.Entry<String, String>> set1 = map.entrySet();
        for (Map.Entry<String, String> entry1 : set1) {
            System.out.println(entry1.getKey() + " : " + entry1.getValue());
        }

        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println(key + " : " + map.get(key));
        }

    }


}
